package com.web.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.annotation.CurrentUser;
import com.web.dao.DemandClassMapper;
import com.web.dao.DemandInfoMapper;
import com.web.dao.UserInfoMapper;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.service.EnterpriseService;
import com.web.service.LoginService;
import com.web.service.SupplyService;
import com.web.service.UserDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户资料
 */
@Slf4j
@RestController
@RequestMapping(value = "/app")
public class UserDataController {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserDataService userDataService;

    @Autowired
    private LoginService loginService;

    @Resource
    private DemandInfoMapper demandInfoMapper;

    @Autowired
    private SupplyService supplyService;
    /**
     * 发布共享资料
     * @param info
     * @return
     */
    @RequestMapping("/data/ShareData")
    public Response addShareData(@CurrentUser UserInfo info, UserData userData){
        UserInfo userInfo = userInfoMapper.findById("user_id",info.getUserId());
        if(userInfo.getIsExpert()!=2){
            return Response.custom().failure("您还不是专家，无法发布资料！");
        }
        userData.setUser_id(info.getUserId());
        userData.setTitle(userData.getTitle());
        userData.setPrice(userData.getPrice());
        userData.setContent(userData.getContent());
        userData.setVideos(userData.getVideos());
        userData.setFirst_video_photos(userData.getFirst_video_photos());
        userData.setPhotos(userData.getPhotos());
        userData.setCreate_time(new Date());
        userData.setModify_time(new Date());
        userDataService.insertSelective(userData);
        return Response.custom().ok();
    }


    /**
     * 我的主页
     * @param info
     * @return
     */
    @RequestMapping("/data/MyHomePage")
    public Response MyHomePage(@CurrentUser UserInfo info){

        Map<String ,Object> map = new HashedMap();
        map.put("UserDetails",userDataService.getUserDetails(info.getUserId()));
        map.put("UserCase",userDataService.getUserCaseList(info.getUserId()));

        return Response.custom().ok().addParams(map);
    }

    /**
     * 修改我的主页
     * @param info
     * @return
     */
    @RequestMapping("/data/ModifyMyHomePage")
    public Response ModifyMyHomePage(@CurrentUser UserInfo info,MyHomePage myHomePage){
        //查询用户基础信息
        UserInfo user = loginService.info("user_id",info.getUserId());
        //查询用户简历
        UserJob userJob = loginService.findUserJob(info.getUserId());
        //用户认证信息
        UserCertification userCertification = loginService.selectCertification("user_id",info.getUserId());
        //查询企业信息
        TechInfo techInfo = loginService.selectTechInfo(info.getUserId());

            if(userJob != null){
                    //修改昵称和头像
                    user.setAvatar(myHomePage.getAvatar());
                    user.setNickname(myHomePage.getNickname());
                    loginService.uploadUserInfo(user);
                    //修改职位
                    userJob.setPosition(myHomePage.getPosition());
                    loginService.modifyJob(userJob);
                if(techInfo != null || userCertification.getType()==2){
                    //修改企业名称
                    userCertification.setTech_name(myHomePage.getTech_name());
                    loginService.uploadCertification(userCertification);
                    //修改企业信息
                    techInfo.setCity(myHomePage.getCity());
                    techInfo.setLogo(myHomePage.getLogo());
                    techInfo.setBusiness(myHomePage.getBusiness());
                    techInfo.setIntroduction(myHomePage.getIntroduction());
                    techInfo.setTechPhotos(myHomePage.getTech_photos());
                    techInfo.setDevicePhotos(myHomePage.getDevice_photos());
                    loginService.updateTtechInfo(techInfo);
                    return Response.custom().ok();
                }
                //您的企业认证还未通过无法修改企业信息！其他信息已修改
                return Response.custom().failure("1");
            }
            //您还没有简历无法修改职位信息！其他信息已修改
            return Response.custom().failure("2");

    }

    /**
     * 个人主页
     * @param user_id
     * @param pageNum
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/data/UserHomePage")
    public Response PersonalHomepage(String user_id,Integer pageNum,Integer type,Integer release_type) throws IOException {
        if(type == null || pageNum == null ||release_type ==null ){
            return Response.custom().failure("缺少请求参数！");
        }
        Map<String,Object> map = new HashedMap();
        map.put("UserDetail",userDataService.getUserDetail(user_id));
        if(type==1){
            PageHelper.startPage(pageNum,10);
            List<UserExperience> list =  loginService.myExperience(user_id);
            PageInfo pageInfo = new PageInfo(list,10);
            if(pageNum > pageInfo.getPages()){
                map.put("UserExperience",new List[0]);
            }else{
                map.put("UserExperience",pageInfo.getList());
            }
        }else if(type == 2){
            //需求
            if (release_type == 1){
                PageHelper.startPage(pageNum,10);
                List<UserDemand>  list =  demandInfoMapper.UserDemand(user_id);
                PageInfo pageInfo = new PageInfo(list,10);
                if(pageNum > pageInfo.getPages()){
                    map.put("UserDemand",new List[0]);
                }else{
                    map.put("UserDemand",pageInfo.getList());
                }
            }else if(release_type == 2){
                //四闲
                PageHelper.startPage(pageNum,10);
                List<FourIdleLists>  list =  supplyService.getUserIdleDetails(user_id);
                PageInfo pageInfo = new PageInfo(list,10);
                if(pageNum > pageInfo.getPages()){
                    map.put("UserIdleDetails",new List[0]);
                }else{
                    map.put("UserIdleDetails",pageInfo.getList());
                }
            }
        }else if(type==3){
            PageHelper.startPage(pageNum,10);
            List<UserCase>  list =  loginService.myCase(user_id);
            PageInfo pageInfo = new PageInfo(list,10);
            if(pageNum > pageInfo.getPages()){
                map.put("UserCase",new List[0]);
            }else{
                map.put("UserCase",pageInfo.getList());
            }
        }
        return Response.custom().ok().addParams(map);
    }

}
